<?php
function L($string)
{
    $param = func_get_args();
    if (empty($param)) {
        return null;
    }

    // 取第一个作为语言名称
    $string = $param[0];
    unset($param[0]);

    $CI =& get_instance();

    // 调用语言包内容
    $lang = $CI->lang->line($string);
    $string = $lang ? $lang : $string;

    // 替换
    $string = $CI->replace_lang($string);

    $string = $param ? vsprintf($string, $param) : $string;

    return $string;
}

function echo_br($string)
{
    echo $string . '<br>';
}

function x_echo($string)
{
    exit($string);
}

function x_json($code, $data = [], $msg = '')
{
    ob_clean();
    header('Content-type: application/json');
    if (is_array($code)) {
        $msg = $data;
        $data = $code;
        $code = 1;
    } else {
        if (is_string($data)) {
            $msg = $data;
            $data = [];
        }
    }
    x_echo(json_encode(array('code' => $code, 'data' => $data, 'msg' => $msg)));
}

function is_valid($type, $value)
{
    switch ($type) {
        case 'email':
            $preg = '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/';
            break;
        case 'phone':
            $preg = '/^1[345789]\d{9}$/';
            break;
        case 'qq':
            $preg = '/[1-9]([0-9]{5,11})/';
            break;
        default:
            return false;
            break;
    }
    return preg_match($preg, $value);
}

function is_mobile()
{
    if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) {
        return true;
    } elseif (isset($_SERVER['HTTP_USER_AGENT'])) {
        $clientkeywords = array(
            'nokia',
            'sony',
            'ericsson',
            'mot',
            'samsung',
            'htc',
            'sgh',
            'lg',
            'sharp',
            'sie-',
            'philips',
            'panasonic',
            'alcatel',
            'lenovo',
            'iphone',
            'ipod',
            'blackberry',
            'meizu',
            'android',
            'netfront',
            'symbian',
            'ucweb',
            'windowsce',
            'palm',
            'operamini',
            'operamobi',
            'openwave',
            'nexusone',
            'cldc',
            'midp',
            'wap',
            'mobile'
        );
        if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
            return true;
        }
    }
    if (isset($_SERVER['HTTP_ACCEPT'])) {
        if (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false && (strpos($_SERVER['HTTP_ACCEPT'],
                    'text/html') === false || strpos($_SERVER['HTTP_ACCEPT'],
                    'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html'))) {
            return true;
        }
    }
    return false;
}

function debug_log($text)
{
    $path = WEBPATH . 'cache/debug/';
    $file = $path . date('y-m-d', SYS_TIME) . '.log';
    if (!is_dir($path)) {
        file_mkdirs($path);
    }
    file_put_contents($file, PHP_EOL . '#' . dr_date(SYS_TIME, 'y-m-d h:i:s') . ' ' . $text, FILE_APPEND);
}

function server_ip()
{
    if (isset($_SERVER['SERVER_ADDR']) && $_SERVER['SERVER_ADDR'] && $_SERVER['SERVER_ADDR'] != '127.0.0.1') {
        return $_SERVER['SERVER_ADDR'];
    }
    return gethostbyname($_SERVER['HTTP_HOST']);
}

function file_mkdirs($dir)
{
    if (!$dir) {
        return false;
    }
    if (!is_dir($dir)) {
        file_mkdirs(dirname($dir));
        if (!is_dir($dir)) {
            mkdir($dir, 0777);
        }
    }
}

function file_mapdirs($source_dir, $directory_depth = 0, $hidden = false)
{
    if ($fp = @opendir($source_dir)) {
        $filedata = array();
        $new_depth = $directory_depth - 1;
        $source_dir = rtrim($source_dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
        while (false !== ($file = readdir($fp))) {
            if ($file === '.' or $file === '..' or $hidden === false && $file[0] === '.' or !@is_dir($source_dir . $file)) {
                continue;
            }
            if (($directory_depth < 1 or $new_depth > 0) && @is_dir($source_dir . $file)) {
                $filedata[$file] = dr_dir_map($source_dir . DIRECTORY_SEPARATOR . $file, $new_depth, $hidden);
            } else {
                $filedata[] = $file;
            }
        }
        closedir($fp);
        return $filedata;
    }
    return false;
}

function ignore_timeout()
{
    @ignore_user_abort(true);
    @set_time_limit(24 * 60 * 60);
    @ini_set('memory_limit', '2028M');
}

function file_delete($fullpath)
{
    if (!@unlink($fullpath)) {
        @chmod($fullpath, 0755);
        if (!@unlink($fullpath)) {
            return false;
        }
    } else {
        return true;
    }
}

function file_deldirs()
{
    if (!file_exists($dir) || !is_dir($dir)) {
        return true;
    }
    if (!($dh = opendir($dir))) {
        return false;
    }
    ignore_timeout();
    while (($file = readdir($dh)) !== false) {
        if ($file != "." && $file != "..") {
            $fullpath = $dir . '/' . $file;
            if (!is_dir($fullpath)) {
                if (!unlink($fullpath)) {
                    chmod($fullpath, 0755);
                    if (!unlink($fullpath)) {
                        return false;
                    }
                }
            } else {
                if (!file_deldirs($fullpath)) {
                    chmod($fullpath, 0755);
                    if (!file_deldirs($fullpath)) {
                        return false;
                    }
                }
            }
        }
    }
    closedir($dh);
    if (rmdir($dir)) {
        return true;
    } else {
        return false;
    }
}

function file_safename($string)
{
    return str_replace(array('..', "/", '\\', ' ', '<', '>', "{", '}', ';', '[', ']'), '', $string);
}

function file_url($url)
{
    if (!$url || strlen($url) == 1) {
        return null;
    } elseif (substr($url, 0, 7) == 'http://' || substr($url, 0, 8) == 'https://') {
        return $url;
    } elseif (strpos($url, SITE_PATH) !== false && SITE_PATH != '/') {
        return $url;
    } elseif (substr($url, 0, 1) == '/') {
        return SITE_PC . substr($url, 1);
    }
    return SYS_ATTACHMENT_URL . $url;
}

function file_get($id)
{
    if (!$id) {
        return '';
    }
    if (is_numeric($id)) {
        $info = attachment_get($id);
        $id = $info['attachment'] ? $info['attachment'] : '';
    }
    $file = file_url($id);
    return $file ? $file : '';
}

function url_current()
{
    $pageURL = 'http';
    if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' || isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443') {
        $pageURL .= 's';
    }
    $pageURL .= '://';
    if (strpos($_SERVER['HTTP_HOST'], ':') !== false) {
        $url = explode(':', $_SERVER['HTTP_HOST']);
        $url[0] ? $pageURL .= $_SERVER['HTTP_HOST'] : ($pageURL .= $url[0]);
    } else {
        $pageURL .= $_SERVER['HTTP_HOST'];
    }
    $pageURL .= $_SERVER['REQUEST_URI'] ? $_SERVER['REQUEST_URI'] : $_SERVER['PHP_SELF'];
    return $pageURL;
}

function url_build($uri, $query = array())
{
    if (substr($uri, 0, 6) == 'member') {
        $self = 'member/';
        $u = substr($uri, 7);
    } else {
        if (substr($uri, 0, 5) == 'admin') {
            $self = 'admin/';
            $u = substr($uri, 6);
        } else {
            $self = '';
            $u = $url;
        }
    }
    $array = explode('/', $u);
    $s = array_shift($array);
    if (is_dir(WEBPATH . 'module/' . $s) || is_dir(WEBPATH . 'app/' . $s)) {
        $url = $s . '/' . array_shift($array) . '/' . array_shift($array);
    } else {
        $url = $s . '/' . array_shift($array);
    }
    if ($array) {
        foreach ($array as $k => $t) {
            $i % 2 == 0 && ($query[$t] = isset($array[$k + 1]) ? $array[$k + 1] : '');
            $i++;
        }
    }
    return SITE_URL . $self . $url . ($query ? '?' . http_build_query($query) : '');
}

function _pages_build($url, $total, $pagesize = SITE_ADMIN_PAGESIZE, $offset = 3)
{
    $url .= strpos($url, '?') === false ? '?' : '&';
    $nums = ceil($total / 10);
    $page = max(1, $_GET['page']);
    $pages['total'] = $total;
    $pages['nums'] = $nums;
    if ($page > $nums) {
        return $pages;
    }
    $page > $offset && ($pages['first'] = _pages_build_link($url, $total, 1));
    $nums - $page > $offset && ($pages['last'] = _pages_build_link($url, $total, $nums));
    isset($page) && $page != 1 && ($pages['prev'] = _pages_build_link($url, $total, $page - 1));
    isset($page) && $page != $nums && ($pages['next'] = _pages_build_link($url, $total, $page + 1));
    if ($page < $offset) {
        $start = 1;
        $end = min($nums, $offset * 2);
    } else {
        if ($page > $offset && $page < $nums - $offset) {
            $start = $page - $offset;
            $end = min($nums, $page + $offset);
        } else {
            $start = max(1, $nums - $offset * 2);
            $end = $nums;
        }
    }
    for ($i = $start; $i <= $end; $i++) {
        $pages['page'][$i] = _pages_build_link($url, $total, $i);
    }
    return $pages;
}

function _pages_build_link($url, $total, $page)
{
    return $url . "total={$total}&page={$page}";
}

function thumb_get($image, $width = null, $height = null, $water = 0, $size = 0)
{
    if (!$image) {
        return THEME_PATH . 'admin/images/nopic.gif';
    }
    if (is_numeric($image)) {
        // 表示附件id
        $thumb = trim(SYS_THUMB_DIR,
                '/') . '/' . md5($image) . '/' . dr_safe_filename("{$width}-{$height}-{$water}-{$size}") . '.jpg';
        if (is_file(MTBASE . $thumb)) {
            return SITE_URL . $thumb;
        }
        $CI =& get_instance();
        return $CI->html_thumb("{$image}-{$width}-{$height}-{$water}-{$size}");
    }
    $image = file_url($image);
    return $image ? $image : THEME_PATH . 'admin/images/nopic.gif';
}

function string2array($data)
{
    if (is_array($data)) {
        return $data;
    } elseif (!$data) {
        return array();
    } elseif (strpos($data, 'a:') === 0) {
        return unserialize(stripslashes($data));
    } else {
        return @json_decode($data, true);
    }
}

function array2string($data)
{
    return $data ? json_encode($data) : '';
}

function member_avatar($uid, $size = '180')
{
    if ($uid) {
        $size = $size > 100 ? 180 : $size;
        foreach (array('png', 'jpg', 'gif', 'jpeg') as $ext) {
            if (is_file(SYS_UPLOAD_PATH . '/member/' . $uid . '/' . $size . 'x' . $size . '.' . $ext)) {
                return SYS_ATTACHMENT_URL . 'member/' . $uid . '/' . $size . 'x' . $size . '.' . $ext;
            }
        }
    }
    return SITE_URL . 'statics/avatar/default.jpg';
}

function qrcode_get($string)
{
    if ($string) {
        $name = md5($string);
        $file = FILEPATH . "qrcode/{$name}.png";
        if (!is_file($file)) {
            require_once LIBRARIES . 'phpqrcode.php';
            QRcode::png($string, $file);
        }
        return SITE_PC . "file/qrcode/{$name}.png";
    }
}

function attachment_get($id)
{
    if (!$id) {
        return null;
    }
    $CI =& get_instance();
    $info = $CI->get_cache_data("attachment-{$id}");
    if ($info) {
        return $info;
    }
    $data = $CI->db->where('id', (int)$id)->get('attachment')->row_array();
    if (!$data) {
        return null;
    }
    $info = $CI->db->where('id', (int)$id)->get('attachment_use')->row_array();
    if (!$info) {
        $info = $CI->db->where('id', (int)$id)->get('attachment_unused')->row_array();
    }
    if (!$info) {
        return null;
    }
    $info = $data + $info;
    $info['_attachment'] = trim($info['attachment'], '/');
    $url = $info['remote'] ? $CI->get_cache('attachment', $data['siteid'], 'data', $info['remote'], 'url') : '';
    $info['attachment'] = $url ? $url . '/' . $info['_attachment'] : attachment_ueditor($info['_attachment']);
    $attachinfo = string2array($info['attachinfo']);
    if (in_array($info['fileext'],
            array('jpg', 'gif', 'png')) && (!isset($attachinfo['width']) || !$attachinfo['width'])) {
        list($attachinfo['width'], $attachinfo['height']) = @getimagesize(file_get($info['attachment']));
    }
    unset($info['attachinfo']);
    $info = $attachinfo ? $info + $attachinfo : $info;
    $CI->set_cache_data("attachment-{$id}", $info, SYS_CACHE_ATTACH);
    return $info;
}

function attachment_ueditor($file)
{
    if (!SYS_UPLOAD_DIR) {
        return $file;
    } elseif (strpos($file, SYS_UPLOAD_DIR) === 0) {
        return trim(str_replace(SYS_UPLOAD_DIR, '', $file), '/');
    } elseif (strpos($file, 'member/uploadfile/') === 0) {
        return trim(str_replace('member/uploadfile/', '', $file), '/');
    } else {
        return $file;
    }
}

function is_memcache()
{
    if (defined('SYS_MEMCACHE') && SYS_MEMCACHE) {
        if (class_exists('Memcached', false)) {
            return true;
        } elseif (class_exists('Memcache', false)) {
            return true;
        }
    }
    return false;
}

function block_get($id, $type = 0, $site = SITE_ID)
{
    $CI = &get_instance();
    return $CI->get_cache('block-' . $site, $id, $type);
}

function safe_replace($string)
{
    $string = str_replace('%20', '', $string);
    $string = str_replace('%20', '', $string);
    $string = str_replace('%27', '', $string);
    $string = str_replace('%2527', '', $string);
    $string = str_replace('*', '', $string);
    $string = str_replace('"', '&quot;', $string);
    $string = str_replace("'", '', $string);
    $string = str_replace('"', '', $string);
    $string = str_replace(';', '', $string);
    $string = str_replace('<', '&lt;', $string);
    $string = str_replace('>', '&gt;', $string);
    return $string;
}

/**
 * @param int $code
 * @param string $msg
 * @param array $data
 * @param string $redirect_url
 * @return false|string
 */
function apiSuccess(int $code = 200, string $msg = "操作成功", array $data = [], string $redirect_url = ''): string
{
    header('Content-Type:application/json');
    $response = ["code" => $code, "msg" => $msg, 'data' => $data];
    if ($redirect_url) {
        $response['redirect'] = $redirect_url;
    }
    exit(json_encode($response, JSON_UNESCAPED_UNICODE));
}

// 返回会员套餐的结束日期
function vipEndTime($info = [], $member)
{
    $time = $member['vip_end'] > time()
        ? strtotime(date('Y-m-d 0:00', $member['vip_end']))
        : strtotime(date('Y-m-d 0:00', time()));

    switch ($info['type']) {
        case 'day':
            $time = strtotime("+{$info['time']} day", $time);
            break;
        case 'month': // 半年
            $time = strtotime("+{$info['time']} month", $time);
            break;
        case 'year': // 年
            $time = strtotime("+{$info['time']} year", $time);
            break;
        case 4: // 永久
            $time = 4294967295;
            break;
        default:
//                $time = strtotime("+{$info['long']} day", $time);
            break;
    }
    return $time;
}

// 格式化字段
function formatField($data): array
{
    if (empty($data)) {
        return [];
    }
    $newData = [];
    foreach ($data as $key => $item) {
        $newData[] = [
            'id' => $key,
            'name' => $item
        ];
    }
    return $newData;
}

function friendNum($number)
{
    $f = [
        '10000' => '万',
        '100000' => '十万',
        '1000000' => '百万',
        '10000000' => '千万',
        '100000000' => '亿'
    ];
    foreach ($f as $k => $v) {
        if ($number < 10000) {
            return $number;
        }
        if (0 != $c = floor($number / (int)$k)) {
            return $c . $v;
        }
    }
}

/*
* 中国正常GCJ02坐标---->百度地图BD09坐标
* 腾讯地图用的也是GCJ02坐标
* @param double $lat 纬度
* @param double $lng 经度
*/
function Convert_GCJ02_To_BD09($lat, $lng): array
{
    $x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    $x = $lng;
    $y = $lat;
    $z = sqrt($x * $x + $y * $y) + 0.00002 * sin($y * $x_pi);
    $theta = atan2($y, $x) + 0.000003 * cos($x * $x_pi);
    $lng = $z * cos($theta) + 0.0065;
    $lat = $z * sin($theta) + 0.006;
    return array('lng' => $lng, 'lat' => $lat);
}

/*
* 百度地图BD09坐标---->中国正常GCJ02坐标
* 腾讯地图用的也是GCJ02坐标
* @param double $lat 纬度
* @param double $lng 经度
* @return array();
*/
function Convert_BD09_To_GCJ02($lat, $lng)
{
    $x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    $x = $lng - 0.0065;
    $y = $lat - 0.006;
    $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * $x_pi);
    $theta = atan2($y, $x) - 0.000003 * cos($x * $x_pi);
    $lng = $z * cos($theta);
    $lat = $z * sin($theta);
    return array('lng' => $lng, 'lat' => $lat);
}


### 获取多文件
function dr_map_thumb($value, $w = 300, $h = 300): array
{
    $data = [];
    $value = @json_decode($value, true);
    if (is_array($value)) {
        foreach ($value as $file) {
            $data[] = ['id' => $file, 'url' => dr_thumb($file, $w, $h)];
        }
    }
    return $data;
}

### 获取多文件
function dr_map_files($value)
{
    if (empty($value)) {
        return false;
    }
    $data = [];
    $value = @json_decode($value, true);
    foreach ($value['file'] as $i => $file) {
        $data[] = array(
            'file' => dr_thumb($file, 550, 850), // 对应文件或附件id
            'title' => $value['title'][$i] // 对应标题描述
        );
    }
    return $data;
}

// 店铺名称
function issuerName($id)
{
    $ci = &get_instance();
    return $ci->models('Issuer')->issuerInfo($id, 'title')['title'];
}

// 生成唯一的邀请码
function createCode(): string
{
    $chars = "AaBbCcDdEFefGHhIiJKLjklMmnopNOqPrQsRtSuTUVWXYZ";
    $str = "";
    for ($i = 0; $i < 6; $i++) {
        $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
    }
    $str .= date('yM') ;
    return $str;
}


/**
 * 写入文件
 * @param $fileName //文件名称
 * @param $buffer // 文件流
 * @return string|null
 */
function bufferToFile($fileName, $buffer): ?string
{
    if (empty($fileName)) {
        return null;
    }
    ob_clean();
    $path = WEBPATH . 'public/wxcode/';
    if (!is_dir($path)) {
        mkdir($path, 0777);
    }
    $filePath = $path . $fileName . '.jpg';
    if (!is_file($filePath)) {
        file_put_contents($filePath, $buffer);
    }
    return SITE_URL . 'wxcode/' . $fileName . '.jpg';
}

// 获取已存在的小程序码
function getMinCode($fileName): ?string
{
    if (empty($fileName)) {
        return null;
    }
    $filePath = WEBPATH . 'public/wxcode/' . md5($fileName) . '.jpg';
    if (is_file($filePath)) {
        return SITE_URL . 'wxcode/' . md5($fileName) . '.jpg';
    } else {
        return null;
    }
}

// 获取当前月份是第几季度
function getSeason(int $month)
{
    return $month % 3 == 0 ? $month / 3 : ceil($month / 3);
}

### 选择框内容
function SelectInfo($key, $value)
{
    $field = dr_field_options_id($key);
    return $field[$value];
}

### 解密
function decrypt($sStr, $sKey)
{
    $sStr = base64_decode($sStr);
    $sKey = hash('sha256', $sKey, true);
    $ivlen = openssl_cipher_iv_length('AES-128-CBC');
    $iv = substr($sStr, 0, $ivlen);
    return unserialize(openssl_decrypt(substr($sStr, $ivlen), 'AES-128-CBC', $sKey, OPENSSL_RAW_DATA, $iv));
}

// 加密
function encrypt($input, $key)
{
    $key = hash('sha256', $key, true);
    $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('AES-128-CBC'));
    return base64_encode($iv . openssl_encrypt(serialize($input), 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv));
}

function payType($type):string
{
    switch ($type){
        case 'wechat':
            return '微信支付';
        case 'test':
            return '测试支付';
        case 'given':
            return '赠送';
        default:
            return '未支付';
    }
}
